package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.CarMoneyActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.CarInformation;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/7/14.
 */

public class CarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    List<CarInformation> mCars;
    private Context mContext;

    public CarAdapter(List<CarInformation> coals, Context context) {
        this.mCars = coals;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mCars.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view ;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mCars.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (mCars.size() > 0) {
            return mCars.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        CardView mCardView;
        TextView statusLabelTV;
        TextView carNumberTV;
        TextView carAddTimeTV;
        TextView driveNameTV;
        TextView driverPhoneTV;
        TextView driverIdNumberTV;


        public ViewHolder(View itemView) {
            super(itemView);
            mCardView = itemView.findViewById(R.id.item_view);
            statusLabelTV = itemView.findViewById(R.id.status_label);
            carNumberTV = itemView.findViewById(R.id.car_number);
            carAddTimeTV = itemView.findViewById(R.id.car_add_time);
            driveNameTV = itemView.findViewById(R.id.driver_name);
            driverPhoneTV = itemView.findViewById(R.id.driver_phone);
            driverIdNumberTV = itemView.findViewById(R.id.driver_id_number);
        }

        public void BindViewData(CarInformation carInfo) {
            carNumberTV.setText(carInfo.getPlatenumbers());
            carAddTimeTV.setText(TimeUtils.parseDateTime(carInfo.getAddtime()));
            driveNameTV.setText(carInfo.getOwner());
            driverPhoneTV.setText(carInfo.getConnectphone());
            driverIdNumberTV.setText(carInfo.getOwneridno());
            if (carInfo.getCartype() == 1) {
                statusLabelTV.setText("内部");
            } else {
                statusLabelTV.setText("外部");
            }
        }

        public void setViewListener(final int position) {
            mCardView.setOnClickListener(view -> {
                switch (view.getId()) {
                    case R.id.item_view:
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("car", mCars.get(position));
                        Intent intent = new Intent(mContext, CarMoneyActivity.class);
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                        break;
                }
            });
        }
    }
}
